// config.go
package componnet

import (
	"github.com/Unknwon/goconfig"
	"os"
	"path/filepath"
)

//环境常量
const ENV_DEV = "dev"
const ENV_TEST = "test"
const ENV_PROD = "prod"

//默认的环境
var ENV = ENV_DEV

var AppConfig *goconfig.ConfigFile
var DbConfig *goconfig.ConfigFile
var MongoConfig *goconfig.ConfigFile

var appConfigFile = "app.ini"
var dbConfigFile = "db.ini"
var mongoConfigFile = "mongo.ini"

//加载配置文件
func loadConfig(configFile string) *goconfig.ConfigFile {
	configPath := getConfigFile(configFile)
	appConfig, err := goconfig.LoadConfigFile(configPath)
	if err != nil {
		panic(err)
	}
	return appConfig
}

//获取配置文件路径
func getConfigFile(configFile string) string {
	rootPath, err := os.Getwd()
	if err == nil {
		realPath, err := (filepath.Abs(rootPath + "/config" + "/" + configFile))
		if err != nil {
			panic(err)
		}
		return realPath
	} else {
		panic(err)
		return ""
	}
}

func init() {
	AppConfig = loadConfig(appConfigFile)
	DbConfig = loadConfig(dbConfigFile)
	MongoConfig = loadConfig(mongoConfigFile)
}
